package com.easytopit.demo.spring.mvc.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.easytopit.demo.spring.mvc.domain.TagUser;
import com.easytopit.demo.spring.mvc.domain.User;

@Controller
@RequestMapping("/tag")
public class TagLIbController {

	@GetMapping("/html")
	public String html(ModelMap modelMap, @ModelAttribute("user") TagUser user) {
		// Map: key-value多选
		HashMap<Integer, String> selects = new LinkedHashMap<>();
		selects.put(0, "请选择");
		selects.put(1, "map-1");
		selects.put(2, "map-2");
		selects.put(3, "map-3");
		modelMap.put("selects", selects);

		// List: 对象属性多选
		ArrayList<Object> users = new ArrayList<>();
		User u = new User();
		u.setId(0);
		u.setName("请选择");
		users.add(u);

		u = new User();
		u.setId(1);
		u.setName("对象-1");
		users.add(u);

		u = new User();
		u.setId(2);
		u.setName("对象-2");
		users.add(u);

		u = new User();
		u.setId(3);
		u.setName("对象-3");
		users.add(u);

		modelMap.put("users", users);

		return "tags/html";
	}

	@PostMapping("/post")
	public String post(ModelMap modelMap, @ModelAttribute("user") TagUser user) {
		// Map: key-value多选
		HashMap<Integer, String> selects = new LinkedHashMap<>();
		selects.put(0, "请选择");
		selects.put(1, "map-1");
		selects.put(2, "map-2");
		selects.put(3, "map-3");
		modelMap.put("selects", selects);

		// List: 对象属性多选
		ArrayList<Object> users = new ArrayList<>();
		User u = new User();
		u.setId(0);
		u.setName("请选择");
		users.add(u);

		u = new User();
		u.setId(1);
		u.setName("对象-1");
		users.add(u);

		u = new User();
		u.setId(2);
		u.setName("对象-2");
		users.add(u);

		u = new User();
		u.setId(3);
		u.setName("对象-3");
		users.add(u);

		modelMap.put("users", users);
		return "tags/html_show";
	}
}
